package your.packag;


import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;

import DAO.MyData;
import DTO.NewAccountDTO;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class AddAccountActivity extends Activity implements OnClickListener {
	
	Spinner curSpinner;
	Spinner accSpinner;
	EditText money;
	EditText name;
	DefaultHttpClient postClient;
	NewAccountDTO newAccountDTO;
	String valuecur,valueacc;
	Button create;
	ProgressDialog dialog;
	List<Cookie> cookies;
	private Handler handler = new Handler() 
	{
		@Override
	    public void handleMessage(Message msg) 
		{
			dialog.dismiss();
	        LoadUI();
	        	

	    }
	};
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addaccount);
		postClient = LoginActivity.postClient;
		dialog = new ProgressDialog(this);
        dialog = ProgressDialog.show(this, "", 
                "Loading. Please wait...", true);
        new Thread(new Runnable() {
			
			  
				  public void run() {
					  LoadData();
					  handler.sendEmptyMessage(0);
				  }}).start();
	}
	
	public void LoadUI()
	{
		curSpinner = (Spinner)findViewById(R.id.addaccountspinner1);
		accSpinner = (Spinner)findViewById(R.id.addaccountspinner2);
		 name = (EditText)findViewById(R.id.addaccountedittext1);
		 money = (EditText)findViewById(R.id.addaccountedittext2);
		 create = (Button)findViewById(R.id.addaccountbutton1);
		 ArrayAdapter<String> account = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, newAccountDTO.account());
			account.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			accSpinner.setAdapter(account);
			ArrayAdapter<String> currency = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, newAccountDTO.currency());
			account.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			curSpinner.setAdapter(currency);
			create.setOnClickListener(this);
			
	}
	public void LoadData() 
	{
		
        
        String xml;
		try {
			/*HttpGet httpGet = new HttpGet("http://smartwebvina.com/smartlive/index.php?option=com_mymonney");
	        HttpResponse response = LoginActivity.postClient.execute(httpGet);
	        HttpEntity httpEntity =response.getEntity();
			xml = EntityUtils.toString(httpEntity);
			org.jsoup.nodes.Document doc = Jsoup.parse(xml);*/
			MyData data = new MyData();
		    newAccountDTO = data.readAddAccount(ManagementActivity.doc);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		
		
	
	}
	public List<Cookie> AddAccount(String url,String name,String money,String curr,String type) {

		   // Setup a HTTP client, HttpPost (that contains data you wanna send) and
		   // a HttpResponse that gonna catch a response.
		 //  postClient = new DefaultHttpClient();
		   HttpPost httpPost = new HttpPost(url);
		   HttpResponse response;
		   List<Cookie> cookies = null;
		  
		

		   try {   
			  
		      // Make a List. Increase the size as you wish.
		      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		      // Add your form name and a text that belongs to the actual form.
		      
		     
		      nameValuePairs.add(new BasicNameValuePair("name", name));
		      nameValuePairs.add(new BasicNameValuePair("balance",money));
		      nameValuePairs.add(new BasicNameValuePair("currency",curr));

		      nameValuePairs.add(new BasicNameValuePair("type",type));

		
		    //  nameValuePairs.add(new BasicNameValuePair("remember", "yes"));
		      

		      nameValuePairs.add(new BasicNameValuePair("option", "com_mymonney"));  
		      nameValuePairs.add(new BasicNameValuePair("datec", "2012-08-15"));
		      nameValuePairs.add(new BasicNameValuePair("task", "create"));
		      nameValuePairs.add(new BasicNameValuePair("view", "parents"));
		     nameValuePairs.add(new BasicNameValuePair("f6f6cb580925b9a2a5f5d2e83482456e", "1"));

		      // Set the entity of your HttpPost.
		      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

		      // Execute your request against the given url and catch the response.
		      response = postClient.execute(httpPost);
		    
		      // Status code 200 == successfully posted data.
		      if(response.getStatusLine().getStatusCode() == 200) {
		         // Green light. Catch your cookies from your HTTP response.
		          cookies =  postClient.getCookieStore().getCookies(); 
		         return  cookies;
		      }
		   } catch (Exception e) {
		   }
		   return  cookies;
	
	}
	public void getValueSpinner()
	{
		String temp = accSpinner.getSelectedItem().toString();
		for(int i =0 ;i< newAccountDTO.account().size(); i++)
		{
			if(temp.compareTo(newAccountDTO.account().get(i))==0)
			{
				valueacc = newAccountDTO.valueacc().get(i);
			}
		}
		
		
		temp = curSpinner.getSelectedItem().toString();
		
		for(int i =0 ;i< newAccountDTO.currency().size(); i++)
		{
			if(temp.compareTo(newAccountDTO.currency().get(i))==0)
			{
				valuecur =newAccountDTO.valuecur().get(i);
			}
		}
		
		
		
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.addaccountbutton1:
				getValueSpinner();
				cookies = AddAccount("http://smartwebvina.com/smartlive/index.php?option=com_mymonney",
						name.getText().toString(), 
						money.getText().toString(),
						valuecur, 
						valueacc);
				break;
		}
		
	}

}
